home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / xinetd / sio.1.5.6 / impl.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-09  |  5.5 KB  |  219 lines

  1. /*
  2.  * (c) Copyright 1992 by Panagiotis Tsirigotis
  3.  * All rights reserved.  The file named COPYRIGHT specifies the terms 
  4.  * and conditions for redistribution.
  5.  */
  6.  
  7. /*
  8.  * $Id: impl.h,v 7.3 1992/06/28 08:45:34 panos Exp $
  9.  */
  10.  
  11. #ifndef SIO_BUFFER_SIZE
  12.  
  13. #include "sioconfig.h"
  14.  
  15. /*
  16.  * MEMORY_MAP must be either #define'd or #undef'd in sioconfig.h
  17.  */
  18. #ifdef MEMORY_MAP
  19. #include <sys/types.h>
  20.  
  21.  
  22. /*
  23.  * A struct map_unit describes a memory mapped area of a file.
  24.  *
  25.  * addr is the address where the file is mapped. If addr is NULL
  26.  * the other fields are meaningless.
  27.  * valid_bytes indicates how many bytes are _valid_ in that unit
  28.  * mapped_bytes of a unit is how many bytes are mapped in that
  29.  * unit ( valid <= mapped ).
  30.  * Normally mapped_bytes will be equal to valid_bytes until
  31.  * we reach the end of the file. Then if the file size is not a multiple
  32.  * of the unit size, only the rest of the file will be mapped at the
  33.  * unit, leaving part of what was mapped at the unit before still
  34.  * visible (this happens because I chose not to unmap a unit before
  35.  * remapping it). In that case valid_bytes shows the size of the "new"
  36.  * mapping and mapped_bytes shows how many bytes are really mapped.
  37.  * mapped_bytes is used in Sdone() to unmap the units.
  38.  */
  39. struct map_unit
  40. {
  41.     caddr_t addr ;
  42.     size_t valid_bytes ;
  43.     size_t mapped_bytes ;
  44. } ;
  45.  
  46.  
  47. /*
  48.  * Meaning of fields used when memory mapping:
  49.  *
  50.  *    file_offset:      it is the offset in the file where the next
  51.  *                      mapping should be done
  52.  *
  53.  *    file_size:        size of the file (obtained from stat(2))
  54.  */
  55. struct mmap_descriptor
  56. {
  57.    off_t file_offset ;
  58.    off_t file_size ;
  59.     struct map_unit first_unit ;
  60.     struct map_unit second_unit ;
  61. } ;
  62.  
  63. typedef struct mmap_descriptor mapd_s ;
  64.  
  65. #endif /* MEMORY_MAP */
  66.  
  67. typedef enum { FAILURE, SUCCESS } status_e ;
  68.  
  69. /*
  70.  * Descriptor management: convert a descriptor pointer to an input or
  71.  * output descriptor pointer
  72.  */
  73. #define IDP( dp )                        (&(dp)->descriptor.input_descriptor)
  74. #define ODP( dp )                        (&(dp)->descriptor.output_descriptor)
  75.  
  76. #define DESCRIPTOR_INITIALIZED( dp )    ((dp)->initialized)
  77.  
  78. /*
  79.  * Internal constants
  80.  */
  81. #define SIO_BUFFER_SIZE           8192
  82. #define SIO_NO_TIED_FD                (-1)
  83.  
  84. typedef enum { NO = 0, YES = 1 } boolean_e ;
  85.  
  86. #ifndef FALSE
  87. #define FALSE            0
  88. #define TRUE            1
  89. #endif
  90.  
  91. #ifndef NULL
  92. #define NULL            0
  93. #endif
  94.  
  95. #ifdef MIN
  96. #undef MIN
  97. #endif
  98. #define MIN( a, b )                    ( (a) < (b) ? (a) : (b) )
  99.  
  100. #define NUL                                '\0'
  101.  
  102. #define PRIVATE                        static
  103.  
  104. #ifdef DEBUG
  105.  
  106. static char *itoa( num )
  107.     unsigned num ;
  108. {
  109. #define NUMBUF_SIZE        15
  110.     static char numbuf[ NUMBUF_SIZE ] ;
  111.     register char *p = &numbuf[ NUMBUF_SIZE ] ;
  112.  
  113.     *--p = '\0' ;
  114.     do
  115.     {
  116.         *--p = num % 10 + '0' ;
  117.         num /= 10 ;
  118.     }
  119.     while ( num ) ;
  120.     return( p ) ;
  121. }
  122.  
  123. #    define ASSERT( expr )                                                        \
  124.         if ( ! (expr) )                                                            \
  125.         {                                                                                \
  126.             char *s1 = "SIO assertion << expr >> failed: File: " ;    \
  127.             char *s2 = __FILE__ ;                                                \
  128.             char *s3 = ", line: " ;                                                \
  129.             char *s4 = itoa( __LINE__ ) ;                                        \
  130.             char *s5 = "\n" ;                                                        \
  131.             (void) write( 2, s1, strlen( s1 ) ) ;                            \
  132.             (void) write( 2, s2, strlen( s2 ) ) ;                            \
  133.             (void) write( 2, s3, strlen( s3 ) ) ;                            \
  134.             (void) write( 2, s4, strlen( s4 ) ) ;                            \
  135.             (void) write( 2, s5, strlen( s5 ) ) ;                            \
  136.             exit ( 1 ) ;                                                            \
  137.         }
  138. #else
  139. #    define ASSERT( expr )
  140. #endif
  141.  
  142.  
  143. #include <errno.h>
  144. extern int errno ;
  145.  
  146. /*
  147.  * IO_SETUP initializes a descriptor if it is not already initialized.
  148.  * It checks if the stream is of the right type (input or output).
  149.  * CONTROL_SETUP checks if the descriptor is initialized and if the
  150.  * stream is of the right type (input or output). 
  151.  *
  152.  *     fd: file descriptor
  153.  *     dp: descriptor pointer
  154.  *     op: operation
  155.  *     ev: error value (if __sio_init fails; __sio_init should set errno) 
  156.  *
  157.  * IO_SETUP will call __sio_init if the descriptor is not initialized.
  158.  * Possible errors:
  159.  *        1. Using CONTROL_SETUP on an uninitialized descriptor.
  160.  *        2. The operation is not appropriate for the descriptor (e.g.
  161.  *            a read operation on an descriptor used for writing). 
  162.  * Both errors set errno to EBADF.
  163.  */
  164. #define CONTROL_SETUP( dp, type, ev )                                                        \
  165.             {                                                                                            \
  166.                 if ( ! DESCRIPTOR_INITIALIZED( dp ) || dp->stream_type != type )    \
  167.                 {                                                                                        \
  168.                     errno = EBADF ;                                                                \
  169.                     return( ev ) ;                                                                    \
  170.                 }                                                                                        \
  171.             }
  172.  
  173.  
  174. #define IO_SETUP( fd, dp, type, ev )                                                        \
  175.             {                                                                                            \
  176.                 if ( DESCRIPTOR_INITIALIZED( dp ) )                                         \
  177.                 {                                                                                        \
  178.                     if ( dp->stream_type != type )                                            \
  179.                     {                                                                                    \
  180.                         errno = EBADF ;                                                            \
  181.                         return( ev ) ;                                                                \
  182.                     }                                                                                    \
  183.                 }                                                                                        \
  184.                 else if ( __sio_init( dp, fd, type ) == SIO_ERR )                        \
  185.                     return( ev ) ;                                                                    \
  186.             }
  187.  
  188.  
  189. /*
  190.  * Internal functions that are visible
  191.  */
  192. int __sio_readf(), __sio_writef(), __sio_pwritef() ;
  193. int __sio_extend_buffer(), __sio_init(), __sio_converter(), __sio_more() ;
  194. status_e __sio_switch() ;
  195.  
  196.  
  197. #ifdef HAS_MEMOPS
  198. #include <memory.h>
  199. #define sio_memcopy( from, to, nbytes )       (void) memcpy( to, from, nbytes )
  200. #define sio_memscan( from, nbytes, ch )      memchr( from, ch, nbytes )
  201. #endif
  202.  
  203. #ifdef HAS_BCOPY
  204. #define sio_memcopy( from, to, nbytes )      (void) bcopy( from, to, nbytes )
  205. #endif
  206.  
  207. #ifndef sio_memcopy
  208. #define sio_memcopy        __sio_memcopy
  209. #define NEED_MEMCOPY
  210. void __sio_memcopy() ;
  211. #endif
  212.  
  213. #ifndef sio_memscan
  214. char *sio_memscan() ;
  215. #endif
  216.  
  217. #endif /* SIO_BUFFER_SIZE */
  218.  
  219.